﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CustomFieldTypes.SharePointRoot.Template.ControlTemplates
{
    public class TableCheckFieldEditor : System.Web.UI.UserControl, IFieldEditor
    {
        protected TextBox txtHeader1Name;
        protected TextBox txtHeader2Name;
        protected TextBox txtHeader3Name;
        protected TextBox txtValues;
        private TableCheckField fldParentField;

        public void InitializeWithField(SPField field)
        {
            this.fldParentField = field as TableCheckField;

            if (this.Page.IsPostBack)
            {
                return;
            }

            //when modifying a field check the correct radio button 
            if (field != null)
            {
                string header1Name = fldParentField.Header1Name;
                string header2Name = fldParentField.Header2Name;
                string header3Name = fldParentField.Header3Name;
                string valuesList = fldParentField.ValuesList;

                txtHeader1Name.Text = header1Name;
                txtHeader2Name.Text = header2Name;
                txtHeader3Name.Text = header3Name;
                txtValues.Text = valuesList;
            }
        }

        //save the value for the mask from the ascx to the field property
        //this is the value the plugin will use to format the masked input
        public void OnSaveChange(SPField field, bool bNewField)
        {
            TableCheckField parentField = (TableCheckField)field;

            parentField.IsNew = bNewField;

            parentField.Header1Name = txtHeader1Name.Text;
            parentField.Header2Name = txtHeader2Name.Text;
            parentField.Header3Name = txtHeader3Name.Text;
            parentField.ValuesList = txtValues.Text;
        }


        // Properties
        public bool DisplayAsNewSection
        {
            get
            {
                return false;
            }
        }
    }
}